package com.example.sunmiv1.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.sunmiv1.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseDialog extends DialogFragment {

    protected Unbinder unbinder;
    // 通用
    protected Intent intent;
    protected boolean isDismiss;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final View view = requireActivity().getLayoutInflater().inflate(getLayoutId(), null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        initListener();
        builder.setView(view);
        return builder.create();
    }

    /* 規定佈局*/
    protected abstract @LayoutRes
    int getLayoutId();

    protected void initView() {
    }

    protected void initData() {
    }

    protected void initListener() {
    }

    /**
     * 在 DialogFragment 中，dialog 窗口 被創建是在 onActivityCreate中，
     * 在此DecorView才被實例化。而我們要設置寬高的參數，
     * 必須在 DecorView實例化之後，不然是沒有效果的。
     */
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            // 下面這些設置必須在此方法(onStart())中才有效
            Window window = dialog.getWindow();
            // 如果不設置這句代碼, 那麼彈框就會與四邊都有一定的距離
            window.setBackgroundDrawableResource(android.R.color.transparent);
            window.setWindowAnimations(R.style.DialogFromBottom);// 設置動畫
            WindowManager.LayoutParams params = window.getAttributes();
            window.setAttributes(setLayout(params));
        }
    }

    // base 鏈式 實驗成功
    public BaseDialog startShow(@NonNull FragmentManager manager, @Nullable String tag) {
        isDismiss = true;
        show(manager, tag);
        return (BaseDialog) this;
    }

    // 回調
    public interface Callback<T> {
        void success(T t);
    }

    protected Callback callback;

    public BaseDialog callback(Callback callback) {
        this.callback = callback;
        return (BaseDialog) this;
    }

    /**
     * 配置params,修改dialog大小，位置等
     * 默認：大小：params.WRAP_CONTENT  位置：Gravity.CENTER
     * <p>
     * 寬度可規定爲固定px！！！
     *
     * @return 重新配置的params
     */
    protected WindowManager.LayoutParams setLayout(WindowManager.LayoutParams params) {
        params.gravity = Gravity.CENTER;
        return params;
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    public int dp2px(int value) {
        final float v = getContext().getResources().getDisplayMetrics().density;
        return (int) (v * value + 0.5f);
    }

}
